<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
      <script type="text/javascript" src="/static/lib/layui/layui.js" charset="utf-8"></script>
      <link rel="stylesheet" href="/static/css/bootstrap-chosen.css">
      <script type="text/javascript" src="/static/js/chosen.jquery.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <!-- 增加 sweetalert -->
  <link rel="stylesheet" href="/static/sweetalert/sweetalert.css">
  <script src="/static/sweetalert/ions_alert.js"></script>
  <script src="/static/sweetalert/sweetalert.min.js"></script>

  <body>
    <div class="x-body">
        <form class="layui-form" action="" lay-filter="component-form-group">
            <div class="layui-form-item">
                <label class="layui-form-label">角色名称</label>
                <div class="layui-input-inline" style="width:200px;">
                    <input type="text" value="{{ .role.RoleName }}" autocomplete="off" class="layui-input" disabled>
                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">角色描述</label>
                <div class="layui-input-inline" style="width:200px;">
                    <input type="text" value="{{ .role.Desc }}" autocomplete="off" class="layui-input" disabled>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">添加用户</label>
                <div class="layui-input-block">
                    <select data-placeholder="请选择..." class="chosen-select" multiple style="width:350px;" tabindex="4" id="role_user_select">
<!--                        <option value="用户1">用户1</option>
                        <option value="用户2">用户2</option>
                        <option value="用户3">用户3</option>
                        <option value="用户4">用户4</option>
                        <option value="用户5">用户5</option>-->
                        {{ range .users }}
                            <option value="{{ .Id }}">{{ .UserName }}</option>
                        {{ end }}
                    </select>
                </div>
            </div>

            <div class="layui-form-item layui-layout-admin">
                <div class="layui-input-block">
                    <div class="layui-footer" >
                            <!--获取角色id-->
                            <!--<input type="hidden" value="{{ .role.Id }}" id="role_id">-->
                        <button class="layui-btn" id="role_user_add_btn">立即提交</button>
                        <a href="#" class="layui-btn layui-btn-primary">返回</a>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <script>
        $('.chosen-select').chosen({width: "100%"});
        // js获取多选框数据：
        //
        // var role_user_select_list=[];
        // //获取多选框的值
        // $("#role_user_select :selected").each(function(){
        //     role_user_select_list.push($(this).val());
        // });
        /* 通过js方式获取 role_id */
        var role_user_add_btn = document.getElementById("role_user_add_btn");   //定义获取变量
        role_user_add_btn.onclick = function (ev) {    // 当点击之后触发动作 function
            var role_id = "{{ .role.Id }}";     // 获取role_id

            // 定义拼接字符串
            var user_ids = "";

            // 拼接字符串内容处理
            var i = 0;

            $("#role_user_select :selected").each(function (){  // 获取role_user_select内选中当标签selected,循环获取到所有选中的each

                // 字符串拼接判断
                if (i == 0) {    // 第一次循环进来不加 ，号
                    user_ids = user_ids + $(this).val()   // 拼接字符串
                } else {
                    user_ids = user_ids + "," + $(this).val()   // 拼接字符串
                }
                i ++
            })
            /*alert(user_ids)
            alert(role_id)*/
            $.ajax({
                url : "{{ urlfor `RoleController.DoRoleUser` }}",
                type : "POST",
                data : {
                    "role_id" : role_id,
                    "user_ids" : user_ids,
                },
                success:function (data) {
                    if (data["code"] == 200) {
                        // 获得frame索引
                        var index = parent.layer.getFrameIndex(window.name);
                        // 关系当前frame
                        parent.layer.close(index);
                        window.location.href = "{{ urlfor `RoleController.List` }}"
/*                        ions_alert.alertSuccess(data["msg"]);
                        setTimeout(reload_list, 2000)   // 定时器*/
                    } else {
                        ions_alert.alertSuccess("插入失败");
                        setTimeout(reload_list, 2000)
                    }
                },
                fail:function (data) {
                    ions_alert.alertSuccess("插入失败");
                    setTimeout(reload_list, 2000)
                }
            })
        };

    </script>

  </body>

</html>